package com.tuju.jetpackfirstdemo.ui.room_demo

import androidx.lifecycle.ViewModel
import com.tuju.jetpackfirstdemo.data.room.User
import com.tuju.jetpackfirstdemo.data.room.UserDao
import io.reactivex.Completable
import io.reactivex.Flowable

class RoomViewModel(private val datasource: UserDao) : ViewModel() {

    fun userName(): Flowable<String> {
        return datasource.getUserById(RoomDemoActivity.USER_ID)
                .map { user -> user.userName }
    }

    fun updateUserName(userName: String): Completable {
        val user = User(RoomDemoActivity.USER_ID, userName)
        return datasource.insertUser(user)
    }

}